package com.xexun.gpstracker.my.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import com.xexun.gpstracker.AppContext;
import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.base.BaseActivity;
import com.xexun.gpstracker.common.CommonRestPath;
import com.xexun.gpstracker.common.Constants;
import com.xexun.gpstracker.fix.swipemenu.SwipeMenu;
import com.xexun.gpstracker.fix.swipemenu.SwipeMenuCreator;
import com.xexun.gpstracker.fix.swipemenu.SwipeMenuListView;
import com.xexun.gpstracker.inter.HttpCallback;
import com.xexun.gpstracker.my.adapter.StepAdapter;
import com.xexun.gpstracker.pojo.ResponseData;
import com.xexun.gpstracker.util.CommUtil;
import com.xexun.gpstracker.util.HttpUtil;
import com.xexun.gpstracker.util.JsonHttpResponseHelper;
import com.xexun.gpstracker.vo.StepCounter;

/**
 *
 */
public class StepCountingActivity extends BaseActivity {

    @ViewInject(R.id.manager_ok_get_data)
    private LinearLayout getData;
    @ViewInject(R.id.manager_no_get_data)
    private RelativeLayout noGetData;

    private SwipeMenuListView commListView;
    private StepAdapter dspAdapter;
    private List<StepCounter> stepList = new ArrayList<>();
    private String termImei;

    @Override
    protected void onDestroy() {
        AppContext.getEventBus().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_step_counting);
        x.view().inject(this);
        AppContext.getEventBus().register(this);

        termImei = getIntent().getStringExtra("IMEI");

        dspAdapter = new StepAdapter(this, stepList, termImei);
        commListView = (SwipeMenuListView) findViewById(R.id.comm_listview);
        commListView.setAdapter(dspAdapter);
        commListView.setMenuCreator(creator);
        commListView.setOnMenuItemClickListener(onMenuItemClickListener);
    }


    @Override
    protected void onResume() {
        loadPage(1);
        super.onResume();
    }

    private SwipeMenuCreator creator = new SwipeMenuCreator() {
        @Override
        public void create(SwipeMenu menu) {
//            int titleSize = 18;
//            SwipeMenuItem downItem = new SwipeMenuItem(AppContext.getContext());
//            downItem.setBackground(new ColorDrawable(Color.parseColor("#9F3131")));
//            downItem.setWidth(Utils.dp2px(AppContext.getContext(), 120f));
//            downItem.setTitle(R.string.re_download);
//            downItem.setTitleSize(titleSize);
//            downItem.setTitleColor(Color.WHITE);
//            downItem.setTag(RE_DOWNLOAD_TAG);
//            menu.addMenuItem(downItem);
        }
    };

    private SwipeMenuListView.OnMenuItemClickListener onMenuItemClickListener = new SwipeMenuListView.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {

            return false;
        }
    };

    private void loadPage(int page) {
        try {
            if (commListView.isShown()) {
                CommUtil.showProcessing(commListView, true, false);
            } else {
                CommUtil.showProcessing(commListView, true, true);
            }
            final JSONObject json = new JSONObject();
            json.put("imei", termImei);
            json.put("page", page);
            HttpCallback callback = new HttpCallback() {
                @Override
                public void onStart() {
                }

                @Override
                public void onFinish() {
                }

                @Override
                public void onSuccess(ResponseData obj) {
                    CommUtil.hideProcessing();
                    if (obj.getRetcode() != 1) {
                        Toast.makeText(StepCountingActivity.this, R.string.load_audio_list_failed, Toast.LENGTH_SHORT).show();
                        getData.setVisibility(View.GONE);
                        noGetData.setVisibility(View.VISIBLE);
                        return;
                    }
                    try {
                        JSONArray array = obj.getBodyArray();
                        if (array != null && array.length() > 0) {
                            stepList.clear();
                            SimpleDateFormat fmt = new SimpleDateFormat("yyy-MM-dd", Locale.getDefault());
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject jsonObject = array.getJSONObject(i);
                                StepCounter st = new StepCounter();
                                st.setStep(jsonObject.optInt("step"));
                                st.setStepId(jsonObject.optLong("stepId"));
                                st.setDate(fmt.parse(jsonObject.optString("createTime")));
                                stepList.add(st);

                            }
                            if (stepList.size() == 0) {
                                getData.setVisibility(View.GONE);
                                noGetData.setVisibility(View.VISIBLE);
                            } else {
                                getData.setVisibility(View.VISIBLE);
                                noGetData.setVisibility(View.GONE);
                            }
                            dspAdapter.notifyDataSetChanged();
                        }
                    } catch (Exception e) {
                        Log.e(Constants.TAG, "获取列表失败！", e);
                    }
                }

                @Override
                public void onFailure(String message) {
                    CommUtil.hideProcessing();
                    Toast.makeText(StepCountingActivity.this, R.string.load_audio_list_failed, Toast.LENGTH_SHORT).show();
                    getData.setVisibility(View.GONE);
                    noGetData.setVisibility(View.VISIBLE);
                }
            };
            HttpUtil.post(CommonRestPath.stepCountingList(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception e) {
            Log.e(Constants.TAG, "获取分页失败！", e);
        }
    }

    @Event(value = R.id.music_listview, type = AdapterView.OnItemClickListener.class)
    private void listItemOnClick(AdapterView<?> parent, View view, int position, long id) {
        commListView.smoothOpenMenu(position);
    }
}
